// ignore_for_file: file_names, slash_for_doc_comments, must_call_super, unnecessary_overrides, override_on_non_overriding_member, must_be_immutable, prefer_const_constructors, sized_box_for_whitespace, avoid_unnecessary_containers, avoid_print

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:mybase/base/abstract/IFFR.dart';
import 'package:mybase/base/abstract/IFFRefresh.dart';
import 'package:mybase/base/abstract/IFFRequestControl.dart';
import 'package:mybase/base/abstract/IFFScaffold.dart';
import 'package:mybase/base/super/FFInk.dart';
import 'package:mybase/base/widget/layout/FFImageButton.dart';
import 'package:mybase/base/widget/layout/FFLineContainer.dart';
import 'package:mybase/base/widget/layout/FFNavi.dart';
import 'package:mybase/base/widget/layout/FFStackContainer.dart';
import 'package:mybase/base/widget/layout/FFTextButton.dart';
import 'package:mybase/base/widget/page/FFScaffold.dart';
import 'package:mybase/base/widgetTool/image/FFImageUtil.dart';

/**普通页面
 * 注意事项
 * 必须继承FFScaffold，FFScaffoldState，必须with MFFScaffold
 * 必须重写 iffScaffold，widget
 * 禁止重写initState,build,
 * 所有的父类属性，方法几乎是ff开头
 */
class FFPage extends FFScaffold {
  String? name;

  FFPage({Key? key, this.name}) : super(key: key);

  @override
  FFPageState createState() => FFPageState();
}

class FFPageState extends FFScaffoldState with MFFScaffold {
  /**固定写法 */
  @override
  IFFScaffold get iffScaffold => this;

  @override
  FFPage get widget => super.widget as FFPage;

  /**固定写法end */
  /**刷新控件，不要刷新功能，则此值不传 */
  @override
  IFFRefresh? get iffRefresh => FFListPageStateRefresh(state: this);

  ///刷新控制

  bool boolValue = false;
  int index = 0;

  @override
  void ffInitState() {
    super.ffInitState();
    // iffScaffold.ffPageStatus = FFPageStatus.result;
  }

  @override
  Widget? ffNavi(BuildContext context) {
    return FFNaviFast.navi(
      context,
      "普通页面",
      actionView: FFTextButton(
        text: "点我",
        alignment: Alignment.center,
        width: 60,
        decoration: BoxDecoration(
          color: Colors.red,
          borderRadius: BorderRadius.all(Radius.circular(30.0)),
        ),
        heigth: FFNaviHeight,
        onClick: () {
          List list = [
            FFPageStatus.firstLoading,
            // FFPageStatus.noData,
            // FFPageStatus.netError,
            FFPageStatus.result
          ];
          index++;
          if (!(index < list.length)) {
            index = 0;
          }
          iffScaffold.ffPageStatus = list[index];
          setState(() {});
        },
      ),
    );
  }

  @override
  Widget? ffFirstLoadingWidget(BuildContext context) {
    return FFTextButton(
      color: Colors.yellow,
      text: "这是第一次菊花提示页",
    );
  }

  /**普通页面建议用CustomScrollView布局，可以配合默认刷新控件一起使用 */
  @override
  Widget? ffBody(BuildContext context) {
    List<Widget> slivers = [
      SliverToBoxAdapter(
        child: Container(
          child: FFLineContainer(
            children: [
              FFImageButton(
                width: 80,
                heigth: 80,
                borderRadius: BorderRadius.circular(20),
                image: FFImageFactory.netWork(
                    "https://t7.baidu.com/it/u=1595072465,3644073269&fm=193&f=GIF"),
              ),
              FFInk(
                child: FFTextButton(
                  notes: "文本展示",
                  padding: EdgeInsets.symmetric(vertical: 10, horizontal: 10),
                  // margin: EdgeInsets.only(left: 10),
                  unconstrainedBox: true,
                  text: "这是按扭",
                  color: Colors.pink,
                  hiddenKeepFrame: boolValue,
                  onClick: () {
                    debugPrint("点击我了");
                  },
                ),
              ),
              SizedBox(height: 40),
              FFInk(
                child: FFTextButton(
                  notes: "多行文本展示",
                  padding: EdgeInsets.symmetric(vertical: 10, horizontal: 10),
                  maxLines: 10,
                  text:
                      "多行文本展示多行文本展示多行文本展示多行文本展示多行文本展示多行文本展示多行文本展示多行文本展示多行文本展示多行文本展示多行文本展示多行文本展示多行文本展示end",
                  color: Colors.pink,
                  onClick: () {
                    debugPrint("点击我了");
                    boolValue = !boolValue;
                    setState(() {});
                  },
                ),
              ),
              SizedBox(height: 40),
              FFImageButton(
                notes: "这是图片网络,带缓存",
                image: FFImageFactory.cacheNetWork(
                    FFImageUtil.utilDefaultNetImage),
                width: 80,
                color: Colors.pink,
              ),
              SizedBox(
                height: 20,
              ),
              FFImageButton(
                notes: "网络图片",
                image: FFImageFactory.netWork(FFImageUtil.utilDefaultNetImage),
                width: 80,
                color: Colors.black,
              ),
              FFImageButton(
                notes: "这是本地图片",
                image: FFImageFactory.asset(
                    "assets/images/common/icon_article_collect_nor@3x.png"),
                width: 40,
                heigth: 40,
                color: Colors.red,
                padding: EdgeInsets.symmetric(vertical: 8, horizontal: 8),
              ),
              SizedBox(
                height: 20,
              ),
              FFLineContainer(
                notes: "线性布局",
                direction: Axis.vertical,
                children: [
                  FFImageButton(
                    image: null,
                    width: 60,
                    heigth: 60,
                    color: Colors.red,
                  ),
                  FFImageButton(
                    image: null,
                    width: 60,
                    heigth: 60,
                    color: Colors.blue,
                  ),
                ],
              ),
              SizedBox(
                height: 20,
              ),
              FFLineContainer(
                direction: Axis.horizontal,
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  FFImageButton(
                    image: null,
                    width: 60,
                    heigth: 60,
                    color: Colors.red,
                  ),
                  FFImageButton(
                    image: null,
                    width: 60,
                    heigth: 60,
                    color: Colors.blue,
                  ),
                ],
              ),
              SizedBox(
                height: 20,
              ),
              FFStackContainer(children: [
                FFImageButton(
                  image: null,
                  width: 140,
                  heigth: 60,
                  color: Colors.red,
                ),
                Positioned(
                  bottom: 0,
                  right: 0,
                  child: FFTextButton(
                    text: "我在红色上面",
                  ),
                ),
              ])
            ],
          ),
        ),
      ),
    ];

    return CustomScrollView(slivers: slivers);
  }


}

class FFListPageStateRefresh with MFFRefresh {
  FFPageState state;
  FFListPageStateRefresh({required this.state});

  @override
  bool? ffCanRefreshHead() {
    return true;
  }

  @override
  ffRefresh(bool isHead) {
    ffRefreshResult(isHead, test, state);
  }

  //具体请求业务
  Future<IFFR> test(IRefreshParam? iRefreshParam) async {
    // 模拟请求调接口
    debugPrint("调用接口...");
    await Future.delayed(Duration(seconds: 2), () {});

    // 模拟到请求的数据
    return R(
      msg: "请求",
      code: "1",
      haveNext: false,
      rStatus: RStatus.result,
    );
  }
}
